/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.action;

import java.io.Serializable;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.data.shared.loader.ListLoadConfig;
import com.sencha.gxt.data.shared.loader.ListLoadResult;


/**
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <B>
 *           the type of the beans returned in the {@link ListLoadResult}
 * @param <C>
 *           the type of the given {@link ListLoadConfig}
 * @param <D>
 *           the type of the RPC returned {@link ListLoadResult}
 */
class DkLoadAction<B extends Serializable, C extends ListLoadConfig, D extends ListLoadResult<B>> extends DkBaseAction<D>
		implements HasProxyAsyncCallback
{

	final DkBaseListLoader<B, C, D> listLoader;
	private AsyncCallback<D> proxyCallback;

	public DkLoadAction(final DkBaseListLoader<B, C, D> listLoader)
	{
		this(null, false, listLoader);
	}

	public DkLoadAction(final String progressMessage, final DkBaseListLoader<B, C, D> listLoader)
	{
		this(progressMessage, true, listLoader);
	}

	public DkLoadAction(final String progressMessage, final boolean showProgress, final DkBaseListLoader<B, C, D> listLoader)
	{
		super(progressMessage, showProgress);
		this.listLoader = listLoader;
	}

	/**
	 * Load called from action.execute()
	 */
	@Override
	protected void run()
	{
		listLoader.loadFromAction(this);
	}

	/**
	 * Load called from proxy.
	 * 
	 * @param loadConfig
	 * @param callback
	 */
	public void load(final C loadConfig, final AsyncCallback<D> callback)
	{
		this.proxyCallback = callback;
		listLoader.doLoad(loadConfig);
	}

	@SuppressWarnings("unchecked")
	@Override
	public AsyncCallback<D> getProxyAsyncCallback()
	{
		return proxyCallback;
	}

}
